package com.StarStudio.Utils;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CodeDensity {

    public static void main(String[] args) {
        double[] arr = {
                129.6875,
                130.0000,
                130.0000,
                130.3125,
                130.9375,
                130.9375,
                131.5625,
                130.9375,
                132.1875,
                132.1875,
                132.5000,
                132.8125,
                133.1250,
                133.1250,
                133.4375,
                133.7500,
                134.0625,
                134.3750,
                134.6875,
                135.0000,
                135.0000,
                135.3125,
                135.6250,
                135.9375,
                136.5625,
                136.8750,
                137.1875,
                137.5000,
                137.8125,
                138.1250,
                138.4375,
                138.4375,
                139.0625,
                139.3750,
                139.6875,
                140.0000


        };
//        BigDecimal sum = BigDecimal.ZERO;
//        for (double v : arr) {
//            sum = sum.add(new BigDecimal(v));
//        }
//        System.out.println(sum.divide(BigDecimal.valueOf(arr.length),4,RoundingMode.HALF_DOWN).doubleValue());
//        System.out.println(Integer.parseInt("01B0",16));
//        try {
//            System.out.println("读取数据：");
//            List<Integer> phaseDataTrans = TDCPhaseDataTrans();
//            TDCPhaseRMS(phaseDataTrans.toArray(new Integer[0]));
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
        int count = 0;
        for (double v : arr) {
            if (count == 18){
                System.out.println("------------------------------------");
            }
            BigDecimal add1 = BigDecimal.valueOf(1.8059);
            System.out.println(add1.add(BigDecimal.valueOf(v)));
            count ++;
        }
    }


    public static double test(){
        int[] arr = {
                5373,
                4455,
                5076,
                6693,
                5076,
                6858,
                4725,
                10287,
                6183,
                5373,
                6345,
                3024,
                3726,
                2916,
                2862,
                2997,
                4428,
                2889,
                3024,
                3512,
                2565,
                4451,
                2835,
                3649,
                3807,
                2978,
                5319,
                3942,
                5805,
                4266,
                4594,
                4941,
                6588,
                4860,
                4455,
                5562,
                5022,
                6048,
                3619,
                81
        };
        double[] arr1 = {
                567.7,
                199.7,
                245.8,
                280.1,
                369.4,
                280.1,
                378.5,
                260.7,
                409.5,
                341.2,
                296.5,
                350.1,
                166.9,
                205.6,
                160.9,
                157.9,
                165.4,
                244.4,
                159.4,
                166.9,
                193.8,
                141.5,
                245.6,
                156.4,
                201.4,
                210.1,
                164.3,
                293.5,
                217.5,
                320.3,
                235.4,
                253.5,
                272.7,
                363.6,
                268.2,
                245.8,
                306.9,
                277.1,
                333.8
        };
        double[] arr2 = {
                //调整后的延迟时间
                567.7,
                277.1,
                333.8,
                199.7,
                245.8,
                280.1,
                369.4,
                280.1,
                378.5,
                260.7,
                409.5,
                341.2,
                296.5,
                350.1,
                166.9,
                420.6,
                160.9,
                157.9,
                165.4,
                244.4,
                159.4,
                166.9,
                193.8,
                141.5,
                245.6,
                156.4,
                201.4,
                210.1,
                164.3,
                293.5,
                217.5,
                390.3,
                235.4,
                253.5,
                272.7,
                363.6,
                268.2,
                245.8,
                306.9
        };
        BigDecimal sum = BigDecimal.ZERO;
        List<Double> list = new ArrayList<>();
        List<Double> sumList = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            BigDecimal divide = new BigDecimal(arr[i]).divide(new BigDecimal(181209),5, RoundingMode.HALF_DOWN);
            double value = divide.multiply(new BigDecimal(10000)).doubleValue();
            list.add(value);
        }
//        for (Double delay : list) {
//            System.out.println(delay);
//            sum += delay;
//        }
        int total = 181209;
        for (double v : arr2) {
            sum = sum.add(new BigDecimal(v));
            sumList.add(sum.doubleValue());
            System.out.println(sum.doubleValue());
        }
        System.out.println("gs:" + arr1.length);
        return sum.doubleValue();
    }

    /**
     * 计算 DNL
     * @Author ChengLee
     * @Date 2023/03/24
     */
    public static void DNL(){
        double[] arr = {
                //调整后的延迟时间
                567.7,
                277.1,
                333.8,
                199.7,
                245.8,
                280.1,
                369.4,
                280.1,
                378.5,
                260.7,
                409.5,
                341.2,
                296.5,
                350.1,
                166.9,
                420.6,
                160.9,
                157.9,
                165.4,
                244.4,
                159.4,
                166.9,
                193.8,
                141.5,
                245.6,
                156.4,
                201.4,
                210.1,
                164.3,
                293.5,
                217.5,
                390.3,
                235.4,
                253.5,
                272.7,
                363.6,
                268.2,
                245.8,
                306.9
        };
        double LSB = 246.8;
        List<Double> DNL = new ArrayList<>();
        for (Double d : arr) {
            double value = ((new BigDecimal(d).subtract(new BigDecimal(LSB))).divide(new BigDecimal(LSB),4,RoundingMode.HALF_DOWN)).doubleValue();
            DNL.add(value);
            System.out.println(value);
        }

    }

    /**
     * 计算 INL
     * @Author ChengLee
     * @Date 2023/03/24
     */
    public static void INL(){
        double O_Delay = 689.5;
        double[] arr = {
                567.7,
                844.8,
                1178.6,
                1378.3,
                1624.1,
                1904.2,
                2273.6,
                2553.7,
                2932.2,
                3192.9,
                3602.4,
                3943.6,
                4240.1,
                4590.2,
                4757.1,
                5177.7,
                5338.6,
                5496.5,
                5661.9,
                5906.3,
                6065.7,
                6232.6,
                6426.4,
                6567.9,
                6813.5,
                6969.9,
                7171.3,
                7381.4,
                7545.7,
                7839.2,
                8056.7,
                8447,
                8682.4,
                8935.9,
                9208.6,
                9572.2,
                9840.4,
                10086.2,
                10393.1
        };
        List<Double> INL = new ArrayList<>();
        double LSB = 246.8;
        for (int i = 0; i < arr.length; i++) {
            double value = BigDecimal.valueOf(arr[i]).subtract(new BigDecimal(O_Delay)).subtract(new BigDecimal(i).multiply(new BigDecimal(LSB))).doubleValue();
            double v = new BigDecimal(value).divide(new BigDecimal(LSB), 4, RoundingMode.HALF_DOWN).doubleValue();
            INL.add(v);
        }
        for (Double a : INL) {
            System.out.println(a);
        }
    }

    /**
     * 计算 INL标准方差
     * @Author ChengLee
     * @Date 2023/03/24
     */
    public static void variance(){
        double[] arr = {
            -0.4935,
            -0.3707,
            -0.0182,
            -0.2091,
            -0.2131,
            -0.0782,
            0.4186,
            0.5535,
            1.0871,
            1.1434,
            1.8027,
            2.1852,
            2.3865,
            2.8051,
            2.4814,
            3.1856,
            2.8375,
            2.4773,
            2.1475,
            2.1378,
            1.7836,
            1.4599,
            1.2451,
            0.8185,
            0.8136,
            0.4473,
            0.2634,
            0.1147,
            -0.2196,
            -0.0304,
            -0.1491,
            0.4323,
            0.3861,
            0.4133,
            0.5182,
            0.9915,
            1.0782,
            1.0741,
            1.3177
        };
        BigDecimal sum = BigDecimal.ZERO;
        double average = 1.0006;
        for (double v : arr) {
            BigDecimal subtract = new BigDecimal(v).subtract(new BigDecimal(average),new MathContext(5, RoundingMode.HALF_DOWN));
            BigDecimal sqrt = subtract.multiply(subtract);
            System.out.println("平方：" + sqrt);
            sum = sum.add(sqrt);
        }
        System.out.println("个数：" + arr.length);
        System.out.println(sum.divide(new BigDecimal(arr.length),4,RoundingMode.HALF_DOWN));//1.0262
    }

    /**
     * 时钟分相法 RMS
     * @Author ChengLee
     * @Date 2023/03/26
     */
    public static void TDCPhaseRMS(Integer[] bins){
        List<Double> measureTime = new ArrayList<>();
        for (int bin : bins) {
            measureTime.add(new BigDecimal(bin).multiply(new BigDecimal("312.5")).doubleValue());
        }
        BigDecimal average,sum = BigDecimal.ZERO;
        for (Double time : measureTime) {
            sum = sum.add(new BigDecimal(time));
            System.out.println(time);
        }
        average = sum.divide(BigDecimal.valueOf(measureTime.size()),4,RoundingMode.HALF_DOWN);
        System.out.println("平均值：" + average);
        List<Double> sumList = new ArrayList<>();
        for (Double time : measureTime) {
            double s = new BigDecimal(time).subtract(average).doubleValue();
            double a = new BigDecimal(s).multiply(new BigDecimal(s)).doubleValue();
            sumList.add(a);
        }
        BigDecimal Sum = BigDecimal.ZERO;
        for (Double s : sumList) {
            Sum = Sum.add(new BigDecimal(s));
        }
        double sqrt = Math.sqrt(Sum.doubleValue());
        double divisor = Math.sqrt(measureTime.size() - 1);
        double res = new BigDecimal(sqrt).divide(new BigDecimal(divisor),4,RoundingMode.HALF_DOWN).divide(BigDecimal.valueOf(Math.sqrt(2)),4,RoundingMode.HALF_DOWN).doubleValue();
        System.out.println("时钟分相法，数据个数为：" + bins.length + ",RMS 结果为：" + res);
    }

    public static List<Integer> TDCPhaseDataTrans() throws IOException {
        Path path = Paths.get("C:\\Users\\92149\\Desktop\\TDC\\毕设TDC\\0401_400MHz_1Hz\\131.2ns.txt");
        byte[] data = Files.readAllBytes(path);
        String result = new String(data, StandardCharsets.UTF_8);
        System.out.println(result);
        String s = result.replaceAll("FF", " ").trim();
        System.out.println("替换后:" + s);
        String s1 = s.replaceAll(" ", "");
        System.out.println("替换后：" + s1);
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < s1.length(); i += 4) {
            String s2 = s1.substring(i, i + 4);
            res.add(Integer.parseInt(s2,16));
        }
        System.out.println(res);
        Map<Integer, Long> collect = res.stream().collect(Collectors.groupingBy(k -> k, Collectors.counting()));
        System.out.println(collect);
        return res;
    }

}
